/**
 iTextUploader
 (C) 2008 MyselftDev (myselftdev@gmail.com)
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; version 2
 of the License.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* Revision: $Id: BrowseView.m 59 2009-01-29 20:25:29Z myselftdev $ */

#import "BrowseView.h"

@implementation BrowseView

- (id)initWithFrame:(CGRect)frame mainView:(MainView*)view dataObject:(PreferencesData*)data
{
	if ((self = [super initWithFrame:frame]))
	{
		mainView = [view retain];
		preferencesData = [data retain];
		
		CGRect contentRect = CGRectMake(0.0, 0.0, 320.0, (460.0 - 49.0));
		
		theWebView = [ [UIWebView alloc] initWithFrame:contentRect];
		theWebView.delegate = self;
		[self addSubview:theWebView];
		
		unlocked = NO;
		
		self.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
		
		[self loadHome];
	}
	return self;
}

-(void) loadHome
{
	if(!unlocked && [[preferencesData getData:@"privacymode"] boolValue])
	{
		TextAlertView *alert = [[TextAlertView alloc] initWithTitle:@"Password" message:@"Privacy mode is enabled. Please insert your password to browse the archive." delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
		alert.textField.secureTextEntry = YES;
		alert.textField.autocorrectionType = UITextAutocorrectionTypeNo;
		alert.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
		[alert show];
		[alert autorelease];
	}
	else
	{
		unlocked = YES;
		
		[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
		
		NSString *requestString = [NSString stringWithFormat:@"username=%@&password=%@", 
								   [[preferencesData getData:@"username"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
								   [[preferencesData getData:@"password"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
		
		NSString *URLString = [NSString stringWithFormat:@"%@%@", @BASEURL, @"app/iphone.php"];
		NSMutableURLRequest *aRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
		[aRequest setHTTPMethod:@"POST"];
		[aRequest setHTTPBody:[requestString dataUsingEncoding:NSUTF8StringEncoding]];
		[theWebView loadRequest:aRequest];
	}
}

- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
	[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
	
	NSURL *errorURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"ConnectionError" ofType:@"html"]];
	NSURLRequest *aRequest=[NSURLRequest requestWithURL:errorURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
	[theWebView loadRequest:aRequest];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
	NSURL *theURL = [request URL];
	NSURL *ourURL = [NSURL URLWithString:@BASEURL];
	
	if([@"file" isEqualToString:[theURL scheme]] || [@"about" isEqualToString:[theURL scheme]] ||
	   [[ourURL host] isEqualToString:[theURL host]])
	{
		return YES;
	}
	
	[[UIApplication sharedApplication] openURL:theURL];
	return NO;
}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	TextAlertView *alertView = (TextAlertView*) actionSheet;
	
	if(buttonIndex > 0 && [[preferencesData getData:@"password"] isEqualToString:alertView.textField.text])
	{
		unlocked = YES;
		[self loadHome];
	}
	else
	{
		UIAlertView *alertMessage = [ [UIAlertView alloc] initWithTitle:@"Incorrect password" message:@"The password you inserted is not correct. To retry, tap again on the Browse tab" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
		[alertMessage show];
		[alertMessage autorelease];
	}
}

-(void)dealloc
{
	[theWebView release];
	[mainView release];
	[preferencesData release];
	
	[super dealloc];
}
@end
